package helpers;

import java.util.TreeMap;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import entities.Domain;

public class CrawlerHelper {
	private static String url = "http://network-tools.com/default.asp?prog=dnsrec&host=";
	
	public static Domain get(Domain domain) {
		String dnsServer = "";
		String dnsRecord = "";
		try {
			TreeMap<String, String> data = new TreeMap<String, String>();
			data.put("terms", domain.getDomain());
			data.put("update_domain", "true");
			
			//Get data
			Document doc = Jsoup.connect(url + domain.getDomain()).userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6").get();
			System.out.println(doc.html());
			//Get dnsRecord
			dnsRecord = doc.select("#AutoNumber2 td:eq(0) div > table").html();
			if (!dnsRecord.isEmpty()) {
				dnsRecord = "<table>" + dnsRecord + "</table>";
				domain.setDnsRecord(dnsRecord);
			}
			
			//Get dnsServer
			doc.select("#AutoNumber2 td:eq(0) div > center, #AutoNumber2 td:eq(0) div > table, #AutoNumber2 td:eq(0) div > p, #AutoNumber2 td:eq(0) div > b").remove();
			dnsServer = doc.select("#AutoNumber2 td:eq(0) div").html();
			if (dnsServer != null) {
				domain.setDnsServer(dnsServer);
			}
			
			
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		return domain;
	}
	
	public static Domain getDns(Domain domain) {
		String dnsServer = "";
		String dnsRecord = "";
		try {
			TreeMap<String, String> data = new TreeMap<String, String>();
			data.put("terms", domain.getDomain());
			data.put("update_domain", "true");
			
			//Get data
			Document doc = Jsoup.connect(url + domain.getDomain()).userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6").get();
			//Get dnsRecord
			dnsRecord = doc.select("#AutoNumber2 td:eq(0) div > table").html();
			if (!dnsRecord.isEmpty()) {
				dnsRecord = "<table>" + dnsRecord + "</table>";
				domain.setDnsRecord(dnsRecord);
			}
			
			//Get dnsServer
			doc.select("#AutoNumber2 td:eq(0) div > center, #AutoNumber2 td:eq(0) div > table, #AutoNumber2 td:eq(0) div > p, #AutoNumber2 td:eq(0) div > b").remove();
			dnsServer = doc.select("#AutoNumber2 td:eq(0) div").html();
			if (dnsServer != null) {
				domain.setDnsServer(dnsServer);
			}
			
			
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		return domain;
	}
	
	public static void main(String[] args) {
		//for (int i = 0; i < 200; i++) {
			System.out.println("-----------------------------------------");
			Domain domain = new Domain();
			/*dns.setDomain("hoantocxoan.com");
			getDns(dns);
			DnsDebug.debug(dns);*/
		//}
	}
}
